package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import com.quan.mall.service.MallAdvertiseItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.MallAdvertiseVo;
import com.quan.mall.entity.MallAdvertise;
import com.quan.mall.mapper.MallAdvertiseMapper;
import com.quan.mall.service.MallAdvertiseService;


@Slf4j
@Service
public class MallAdvertiseServiceImpl extends MyBaseServiceImpl<MallAdvertiseMapper, MallAdvertise> implements MallAdvertiseService {

    @Autowired
	private MallAdvertiseMapper mapper;

    @Autowired
    private MallAdvertiseItemService mallAdvertiseItemService;

	@Override
    public List<MallAdvertiseVo> queryList(PageUtils pageUtils, MallAdvertiseVo vo) {
        QueryWrapper<MallAdvertise> queryWrapper = new QueryWrapper<MallAdvertise>(vo);

        List<MallAdvertise> list = super.list(pageUtils, queryWrapper);
        List<MallAdvertiseVo> collect = list.stream().map(item -> {
            MallAdvertiseVo bean = new MallAdvertiseVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, MallAdvertiseVo vo) {
        QueryWrapper<MallAdvertise> queryWrapper = new QueryWrapper<MallAdvertise>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<MallAdvertise> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<MallAdvertise> records = page.getRecords();

        final List<MallAdvertise> collect = records.stream().map((item) -> {
            MallAdvertiseVo bean = new MallAdvertiseVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public MallAdvertiseVo showAdvertise(String identity) {
        return this.mapper.showAdvertise(identity);
    }
}